home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_493 / bbformat / lib / mytdio.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  193 lines

  1. /*
  2.  * MyTDIO.c
  3.  *
  4.  * Handles a trackdisk RAW read with WORDSYNC.
  5.  * I can't get the ETD_RAWREAD to work, so coded one myself. DAV
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/nodes.h>
  10. #include <exec/lists.h>
  11. #include <exec/memory.h>
  12. #include <exec/interrupts.h>
  13. #include <exec/ports.h>
  14. #include <exec/libraries.h>
  15. #include <exec/io.h>
  16. #include <exec/tasks.h>
  17. #include <exec/execbase.h>
  18. #include <exec/devices.h>
  19. #include <devices/trackdisk.h>
  20. #include <hardware/custom.h>
  21. #include <hardware/cia.h>
  22. #include <hardware/intbits.h>
  23. #include <hardware/adkbits.h>
  24. #include <hardware/dmabits.h>
  25. #include <resources/disk.h>
  26.  
  27. #define __NO_PRAGMAS
  28. #include <functions.h>
  29.  
  30. #define CIAF_DSKSELALL (CIAF_DSKSEL3 | CIAF_DSKSEL2\
  31.             | CIAF_DSKSEL1 | CIAF_DSKSEL0)
  32.  
  33. #ifndef NUMHEADS
  34. #define NUMHEADS 2
  35. #endif
  36.  
  37. extern struct Custom custom;
  38. extern struct CIA ciaa, ciab;
  39.  
  40. /* Above doesn't seem to work with Aztec, small data model, so ... */
  41.  
  42. #define CUSTOMADD    ((struct Custom *)0xdff000)
  43. #define CIAAADD        ((struct CIA *)0xbfe001)
  44. #define CIABADD        ((struct CIA *)0xbfd000)
  45.  
  46. /*
  47.  * Disable/Enable interrupts at the CPU
  48.  */
  49. static APTR sysStack;
  50. static ULONG oldSR;
  51.  
  52. cpuDisable()
  53. {
  54.     oldSR = SetSR(0x700L,0x700L);
  55.     sysStack = SuperState();
  56. }
  57. cpuEnable()
  58. {
  59.     if(sysStack) UserState(sysStack);    
  60.     SetSR(oldSR,0x700L);
  61. }
  62.  
  63.  
  64. /* make a special read request up */
  65.  
  66. MyTDIO(struct IOExtTD *tdreq)
  67. {
  68.     struct Custom *pcustom = CUSTOMADD;    /*&custom;*/
  69.     struct CIA *pciaa = CIAAADD;        /*&ciaa;*/
  70.     struct CIA *pciab = CIABADD;        /*&ciab;*/
  71.  
  72.     UWORD command;
  73.     ULONG actual,length,offset;
  74.     APTR data;
  75.     UBYTE flags;
  76.     int drive,head;
  77.     UWORD dsklenw;
  78.     long timeout;
  79.  
  80.     struct IOStdReq *req = (struct IOStdReq *) tdreq;
  81.  
  82.     if(!(req->io_Command == ETD_RAWREAD || req->io_Command == TD_RAWREAD)) {
  83.         req->io_Error = 100;
  84.         return;
  85.     }
  86.  
  87.     /* our request - local copy parameters */
  88.     command = req->io_Command;
  89.     flags = req->io_Flags;
  90.     actual = req->io_Actual;    /* SPECIAL - expect drive unit # here */
  91.     length = req->io_Length;
  92.     data = req->io_Data;
  93.     offset = req->io_Offset;    /* Track (not cyl) */
  94.  
  95.     drive = actual;
  96.     head  = offset % (NUMSECS*TD_SECTOR);
  97.  
  98.     /* Seek to track */
  99.  
  100.     req->io_Offset = offset * (NUMSECS*TD_SECTOR);
  101.     req->io_Command = TD_SEEK;
  102.     DoIO((struct IORequest *)req);
  103.     if(req->io_Error != 0) {
  104.         req->io_Error = 101;
  105.         return;
  106.     }
  107.  
  108.     /* Do raw - read magic */
  109.  
  110.     cpuDisable();
  111.  
  112.     pcustom->dmacon = DMAF_SETCLR | DMAF_DISK | DMAF_MASTER;
  113.  
  114.     /* turn OFF bits - not GCR, and no pre-comp. for now */ 
  115.     pcustom->adkcon = ADKF_PRECOMP0|ADKF_PRECOMP1|ADKF_MSBSYNC|ADKF_WORDSYNC;
  116.     /* turn ON bits - mfm */
  117.     pcustom->adkcon = ADKF_SETCLR|ADKF_WORDSYNC|ADKF_MFMPREC|ADKF_FAST;
  118.     /* turn ON wordsync if required */
  119.     if(flags & IOTDF_WORDSYNC)
  120.         pcustom->adkcon = ADKF_SETCLR|ADKF_WORDSYNC;
  121.  
  122.     
  123.     pcustom->dsksync = 0x4489;
  124.  
  125.     pcustom->dsklen = DSKDMAOFF;    /* 0x4000 */
  126.     pcustom->dskpt = data;
  127.     dsklenw = 0x8000 | ((length >> 1) & 0x3fff);
  128.     pciab->ciaprb &= ~CIAF_DSKMOTOR;    /* ensure motor on */
  129.  
  130.     if(drive == 0) 
  131.         pciab->ciaprb &= ~CIAF_DSKSEL0;
  132.     else if(drive == 1)
  133.         pciab->ciaprb &= ~CIAF_DSKSEL1;
  134.     else if(drive == 2)
  135.         pciab->ciaprb &= ~CIAF_DSKSEL2;
  136.     else if(drive == 3)
  137.         pciab->ciaprb &= ~CIAF_DSKSEL3;
  138.  
  139.     if(head == 0) 
  140.         pciab->ciaprb &= ~CIAF_DSKSIDE;
  141.     else
  142.         pciab->ciaprb |=  CIAF_DSKSIDE;
  143.  
  144.     pcustom->dsklen = dsklenw;        /* first write */
  145.  
  146.     timeout = 0;
  147.     while(pciaa->ciapra & CIAF_DSKRDY) {    /* wait for ready */
  148.         if(++timeout > 1000000L) {
  149.             pcustom->dsklen = DSKDMAOFF;
  150.             pciab->ciaprb |= (CIAF_DSKSELALL);
  151.             cpuEnable();
  152.             req->io_Error = 102;
  153.             return;    /* timed out waiting for ready */
  154.         }
  155.     }
  156.  
  157.     if(flags & IOTDF_INDEXSYNC) {
  158.         timeout = pciab->ciaicr;
  159.         timeout = 0;
  160.         while(!(pciab->ciaicr & CIAICRF_FLG)) {    /* wait for index pls */
  161.             if(++timeout > 1000000L) {
  162.                 pcustom->dsklen = DSKDMAOFF;
  163.                 pciab->ciaprb |= (CIAF_DSKSELALL);
  164.                 cpuEnable();
  165.                 req->io_Error = 103;
  166.                 return;    /* timed out waiting for index */
  167.             }
  168.         }
  169.     }
  170.  
  171.     pcustom->intena = INTF_SETCLR | INTF_DSKBLK;
  172.     pcustom->intreq = INTF_DSKBLK;        /* clear pending DMA-done */
  173.     pcustom->dsklen = dsklenw;        /* second write - start DMA*/
  174.     timeout = 0;
  175.     while(!(pcustom->intreqr & INTF_DSKBLK)) { /* wait for DMA done */
  176.         if(++timeout > 1000000L) {
  177.             pcustom->dsklen = DSKDMAOFF;
  178.             pciab->ciaprb |= (CIAF_DSKSELALL);
  179.             cpuEnable();
  180.             req->io_Error = 104;
  181.             return;    /* timed out waiting for DMA */
  182.         }
  183.     }
  184.     pciab->ciaprb |= (CIAF_DSKSELALL);
  185.     pcustom->dsklen = DSKDMAOFF;
  186.     pcustom->intreq = INTF_DSKBLK;        /* clear DMA-done */
  187.     cpuEnable();
  188.     req->io_Error = 0;
  189.  
  190.     return;
  191. }
  192.  
  193.